package com.tianchi.lexiang.util;

import java.util.List;

import android.util.Log;

import com.tianchi.lexiang.view.GridViewWithPosition;

public abstract class ScreenPostionStateManager {
	private static final String TAG = "ScreenPostionStateManager";
	public enum Direction{LEFT,RIGHT,UNKNOWN};
	
	public abstract void refreshPreScreenContent(GridViewWithPosition grid);
	public abstract void refreshNextScreenContent(GridViewWithPosition grid);
	
	private void refreshContentAccordingDirection(Direction direction,List<GridViewWithPosition> allScreen){
		switch (direction) {
			case LEFT:
				for(GridViewWithPosition grid : allScreen){
					switch (grid.getPosition()) {
					case NEXT:
						refreshNextScreenContent(grid);
						break;
					default:
						break;
					}
				}
				break;
			case RIGHT:
				for(GridViewWithPosition grid : allScreen){
					switch (grid.getPosition()) {
					case PRE:
						refreshPreScreenContent(grid);
						break;
					default:
						break;
					}
				}
				break;
			default:
				break;
		}
	}
	
	public void runScreenPostionStateMachine(Direction direction,List<GridViewWithPosition> allScreen){
		switch (direction) {
		case LEFT:
			for (GridViewWithPosition grid : allScreen) {
				switch (grid.getPosition()) {
				case CURRENT:
					grid.setPosition(ScreenPostion.PRE);
					break;
				case PRE:
					grid.setPosition(ScreenPostion.NEXT);
					break;
				case NEXT:
					grid.setPosition(ScreenPostion.CURRENT);
					break;
				default:
					break;
				}
			}
			break;
		case RIGHT:
			for (GridViewWithPosition grid : allScreen) {
				switch (grid.getPosition()) {
				case CURRENT:
					grid.setPosition(ScreenPostion.NEXT);
					break;
				case PRE:
					grid.setPosition(ScreenPostion.CURRENT);
					break;
				case NEXT:
					grid.setPosition(ScreenPostion.PRE);
					break;
				default:
					break;
				}
			}
			break;
		default:
			break;
		}
		dumpState(allScreen);
		refreshContentAccordingDirection(direction,allScreen);
	}
	
	private void dumpState(List<GridViewWithPosition> allScreen){
		StringBuilder builder = new StringBuilder();
		for (GridViewWithPosition grid : allScreen) {
			builder.append("screen="+grid.getTag()+",position="+grid.getPosition()+"\n");
		}
		Log.d(TAG, builder.toString());
	}
}
